package com.athuiju.fenxiaoback.controller;


import com.athuiju.fenxiaoback.entity.TbCartitem;
import com.athuiju.fenxiaoback.entity.vo.ItemNotesVo;
import com.athuiju.fenxiaoback.service.TbCartitemService;
import com.athuiju.fenxiaoback.utils.JwtUtils;
import com.athuiju.fenxiaoback.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author athuiju
 * @since 2021-07-02
 */
@Api(description = "购物车项",tags = "购物车项")
@RestController
@RequestMapping("/fenxiaoback/tb-cartitem")
public class TbCartitemController {

    @Autowired
    TbCartitemService cartitemService;

    @ApiOperation(value = "购物项数量修改")
    @GetMapping("/itemNumUpdate/{skuId}/{num}")
    public R addCart(@PathVariable("skuId") Long skuId, @PathVariable("num") Integer num, HttpServletRequest request){
        boolean b = JwtUtils.checkToken(request);

        if (b){
            String memberid = JwtUtils.getMemberIdByJwtToken(request);

            QueryWrapper<TbCartitem> itemWrapper=new QueryWrapper<>();
            itemWrapper.eq("memberid",memberid);
            itemWrapper.eq("skuId",skuId);
            TbCartitem cartitem=cartitemService.getOne(itemWrapper);
            cartitem.setCount(num);

            cartitemService.update(cartitem,itemWrapper);

            cartitemService.calcTotalPrice(skuId,memberid);
            return R.ok();

        }else{
            return R.error("请登录");
        }
    }


    @ApiOperation(value = "购物项添加/修改留言")
    @PostMapping("/addItemNotes")
    public R addCart(@RequestBody ItemNotesVo itemNotesVo, HttpServletRequest request) throws ExecutionException, InterruptedException {

        boolean b = JwtUtils.checkToken(request);

        if (b){
            String memberid = JwtUtils.getMemberIdByJwtToken(request);

            QueryWrapper<TbCartitem> itemWrapper=new QueryWrapper<>();
            itemWrapper.eq("memberid",memberid);
            itemWrapper.eq("skuId",itemNotesVo.getSkuId());
            TbCartitem cartitem=cartitemService.getOne(itemWrapper);
            cartitem.setNotes(itemNotesVo.getNotes());

            cartitemService.update(cartitem,itemWrapper);


            return R.ok();

        }else{
            return R.error("请登录");
        }


    }

    @ApiOperation(value = "计算选中项总价")
    @PostMapping("/calcCheckItemTotalPrice")
    public R calcCheckItemTotalPrice(@RequestBody List<String> skuIds, HttpServletRequest request){
        boolean b = JwtUtils.checkToken(request);
        //
        if (b){
            String memberid = JwtUtils.getMemberIdByJwtToken(request);
            String totalPrice = cartitemService.calcCheckItemTotalPrice(skuIds, memberid);
            //计算失败
            if ("计算失败".equals(totalPrice.toString())){
                return R.error().put("msg","计算失败");
            }
            return R.ok().put("totalPrice",totalPrice);

        }else{
            return R.error("请登录");
        }

    }

}

